/*
 * 文  件   名: UserFriendController.java
 * 版         本: docrepository-web(Anttribe). All rights reserved
 * 描         述: <描述>
 * 修   改  人: zhaoyong
 * 修改时 间: 2014年11月17日
 */
package org.anttribe.docrepository.web.friend.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.anttribe.docrepository.common.UnifyException;
import org.anttribe.docrepository.common.constants.Keys;
import org.anttribe.docrepository.friend.domain.UserFriend;
import org.anttribe.docrepository.friend.domain.UserGroup;
import org.anttribe.docrepository.friend.manager.FriendManager;
import org.anttribe.docrepository.user.domain.UserInfo;
import org.anttribe.docrepository.web.friend.convert.UserFriendConvert;
import org.anttribe.saturn.common.model.Result;
import org.anttribe.saturn.common.utils.ObjectUtils;
import org.anttribe.saturn.component.dataparser.DataParser;
import org.anttribe.saturn.runtime.stereotype.Widget;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 用户好友管理
 * 
 * @author zhaoyong
 * @version 2014年11月17日
 */
@Widget(name = "UserFriendWidget")
@RequestMapping("/user/friends")
public class UserFriendWidget
{
    /**
     * LOGGER
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(UserFriendWidget.class);
    
    /**
     * friendManager
     */
    @Autowired
    private FriendManager friendManager;
    
    /**
     * dataParser数据解析器
     */
    private DataParser dataParser = DataParser.getDataParser(DataParser.Parser.Json.name());
    
    /**
     * convert
     */
    private UserFriendConvert convert = new UserFriendConvert();
    
    /**
     * 用户群组列表
     * 
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @param httpSession HttpSession
     * @return ModelAndView
     */
    @RequestMapping("/listGroups")
    public ModelAndView doUserGroupsList(HttpServletRequest request, HttpServletResponse response,
        HttpSession httpSession)
    {
        ModelAndView mv = new ModelAndView();
        
        // 从session中获取用户
        UserInfo userInfo = (UserInfo)httpSession.getAttribute(Keys.USER_SESSION);
        if (!ObjectUtils.isNullOrNone(userInfo))
        {
            // 用户群组列表
            List<UserGroup> userGroupsList = null;
            try
            {
                userGroupsList = friendManager.listUserGroup(userInfo.getUserId());
            }
            catch (UnifyException e)
            {
                LOGGER.warn("While getting user groups, get error: {}", e.getErrorNo());
                mv.addObject("errorNo", e.getErrorNo());
            }
            mv.addObject("userGroupsList", userGroupsList);
        }
        mv.setViewName("friend/userGroups");
        
        return mv;
    }
    
    /**
     * 用户好友列表
     * 
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @param httpSession HttpSession
     * @throws IOException
     */
    @RequestMapping("/listUserFriends")
    public void doUserFriendList(HttpServletRequest request, HttpServletResponse response, HttpSession httpSession)
        throws IOException
    {
        // 从session中获取用户
        UserInfo userInfo = (UserInfo)httpSession.getAttribute(Keys.USER_SESSION);
        if (!ObjectUtils.isNullOrNone(userInfo))
        {
            // 用户好友列表
            List<UserFriend> userFriendsList = null;
            try
            {
                userFriendsList = friendManager.listUserFriend(request.getParameter("groupId"), userInfo.getUserId());
            }
            catch (UnifyException e)
            {
                LOGGER.warn("While getting user friends, get error: {}", e.getErrorNo());
            }
            
            // 输出json
            String jsonString = "[]";
            if (!CollectionUtils.isEmpty(userFriendsList))
            {
                jsonString = dataParser.parserToString(convert.convertToVo(userFriendsList), null);
            }
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.write(jsonString);
        }
    }
    
    /**
     * 编辑/修改用户好友备注姓名
     * 
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @param httpSession HttpSession
     * @throws IOException
     */
    @RequestMapping("/editUserFriendName")
    public void doEditUserFriendName(HttpServletRequest request, HttpServletResponse response, HttpSession httpSession)
        throws IOException
    {
        String userFriendId = request.getParameter("userFriendId");
        if (!StringUtils.isEmpty(userFriendId))
        {
            Result<String> result = new Result<String>();
            String friendName = request.getParameter("friendName");
            if (!StringUtils.isEmpty(friendName))
            {
                try
                {
                    friendManager.editFriendName(userFriendId, friendName);
                }
                catch (UnifyException e)
                {
                    result.setResultCode(e.getErrorNo());
                    LOGGER.warn("While editing user friend's name, get error: {}", e.getErrorNo());
                }
            }
            
            // 输出json
            String jsonString = dataParser.parserToString(result, Result.class);
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.write(jsonString);
        }
    }
    
    /**
     * 删除好友
     * 
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @param httpSession HttpSession
     * @throws IOException
     */
    @RequestMapping("/removeUserFriend")
    public void doRemoveUserFriend(HttpServletRequest request, HttpServletResponse response, HttpSession httpSession)
        throws IOException
    {
        String userFriendId = request.getParameter("userFriendId");
        if (!StringUtils.isEmpty(userFriendId))
        {
            Result<String> result = new Result<String>();
            try
            {
                friendManager.removeUserFriend(userFriendId);
            }
            catch (UnifyException e)
            {
                result.setResultCode(e.getErrorNo());
                LOGGER.warn("While removing user friend's name, get error: {}", e.getErrorNo());
            }
            
            // 输出json
            String jsonString = dataParser.parserToString(result, Result.class);
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.write(jsonString);
        }
    }
}